<?php
$isReloadable = true;
error_reporting(E_ALL);
require_once("../../../pers/inc.config.php");
require_once OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php';
require_once OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php';
require_once '../_inc/class.wfmModulo.php';

$oMod = new wfmModulo(reqVarInt('id'));
if ($oMod->codModulo == "") {
	backOnErr("id.'".reqVar('id')."' errato");
	exit;
}
$sWhere = " ";
$sOrder = "";
$idModulo = $oMod->idModulo;
$codModulo = $oMod->codModulo;

$sSql = "SELECT count(*) AS num_rec " .
		" FROM wfm_rec_$codModulo r $sWhere $sOrder";
//dbg_($sSql); 
$objDB->query($sSql,'selRec');
$acRec = $objDB->fetch('selRec');
$numRec = $acRec['num_rec'];
$link = $_SERVER['PHP_SELF']."?id=$idModulo&".$sXSNQS;
$startOffset = 0;
$endOffset = 0;
$sFiltro = ovfGetFiltro($link);
$sPaginaz = ovfGetPaginaz($link, $numRec, $startOffset, $endOffset);

$sSql = "SELECT r.id_rec " .
		" FROM wfm_rec_$codModulo r $sWhere $sOrder".
		" LIMIT ".$startOffset.",".($endOffset-$startOffset);
$objDB->query($sSql,'selRec');

$cmdL = "";
$cmdL = "<a href=\"recIns.php?id=$idModulo&$sXSNQS\" class=\"cmd1\">nuova registrazione</a>";

$cmdR = "";
$cmdR .= cmdTorna(); 

testaHtml($oMod->titolo,'Lista', $msgPag, 'wfma');
echo getHtmlForm();
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('startoffset');
?>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
			<?=$sFiltro?>
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
</table>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <?php echo getListaLab($oMod);?>
   </tr>
<? 
$clRF = 'txt2';
while($acZ = $objDB->fetch('selRec')) { 
	echo "<tr>\n";
	echo getListaDat($oMod,$acZ['id_rec'],$clRF);
   echo "</tr>\n";
 } 
?>
</table>
<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			&nbsp;
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
         <?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         <?=$cmdR?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function getListaLab(&$oMod) {
	global $objSess, $objDB, $sXSNQS;
	$idModulo = $oMod->idModulo;
	$codModulo = $oMod->codModulo;
	$pagSessVar = "lista_$codModulo";
	$campoOrd = $objSess->setReqSV($pagSessVar,'campoOrd','');
	$tipoOrd = $objSess->setReqSV($pagSessVar, 'tipoOrd', 'ASC');
	$sHtml = '';
	//dbg_("getHtmlLab: $pagSessVar, $sQS");
	dbg_($oMod);
	$aCampiList = $oMod->getCampiList();
	foreach($aCampiList as $seqX => $sNomX) {
		$aCX = $oMod->aCampi[$sNomX];
		if ($campoOrd == "") {
			$objSess->setSV($pagSessVar,'campoOrd',$sNomX);
			$campoOrd = $sNomX;
		}
		$sHtml .= "<td class='lab1' align='center'>";
		$sHref = $_SERVER['PHP_SELF']."?id=$idModulo&campoOrd=$campoOrd&tipoOrd=$tipoOrd&$sXSNQS";
		if ($sNomX != $campoOrd) {
			$sHtml .= "<a href='$sHref&tipoOrd=ASC' class='lab1'>".$aCX['lab']."</a></td>";
			continue;
		}
		if ($tipoOrd=='DESC') {
			$sHtml .= "<a href='$sHref&tipoOrd=ASC' class='lab1'>";
			$sHtml .= "<img src='".OVF_URL_ROOT."/app/_img/freccia_giu.gif' border='0'/>&nbsp;";
		} else {
			$sHtml .= "<a href='$sHref&tipoOrd=DESC' class='lab1'>";
			$sHtml .= "<img src='".OVF_URL_ROOT."/app/_img/freccia_su.gif' border='0'/>&nbsp;";
		}		
		$sHtml .= $aCX['lab']."</a></td>";
	}	
	$sHtml .= "<td class='lab1' style='text-align:center'>Stato</td>";
	$sHtml .= "<td class='lab1' style='text-align:center'>vis.</td>";
	$sHtml .= "<td class='lab1' style='text-align:center;color:red;'>ZAP</td>";
	return $sHtml;
}

function getListaDat(&$oMod,$idRec,&$clRF) {
	global $objSess, $objDB, $sXSNQS;
	$sHtml = "";
	$idModulo = $oMod->idModulo;
	$codModulo = $oMod->codModulo;
	$pagSessVar = "lista_$codModulo";
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
	$sSql = "SELECT r.*, p.cod_pagina, s.descr AS stato_descr FROM wfm_rec_$codModulo r "
		." LEFT JOIN wfm_stati s ON s.id_modulo = $idModulo AND s.stato = r.stato "
		." LEFT JOIN wfm_pagine p ON p.id_pagina = r.id_pagina "
		." WHERE r.id_rec = ".$idRec;
	$objDB->query($sSql,'selRecX');
	$aRec = $objDB->fetch('selRecX');
	//dbg_($aRec);
	$sHref = "recMod.php?id=$idModulo&idr=".$aRec['id_rec']."&$sXSNQS";
	$tagA1 = "<a href=\"$sHref\" class=\"$clRF\">";
	$tagA2 = "</a>";
	$sHtml = '';
	$aCampiList = $oMod->getCampiList();
	foreach($aCampiList as $seqX => $sNomX) {
		$aCX = $oMod->aCampi[$sNomX];
		$val = ovfVx($aRec,$sNomX);
		switch($aCX['tip']) {
			case 'data':
				$val = YMD2DMY($val);
				$align = 'center';
				break;
			case 'numero':
				$val = num2str($val,$aCX['dec'])."&nbsp;";
				$align = 'right';
				break;
			case 'intero':
				$val = num2str($val,0)."&nbsp;";
				$align = 'right';
				break;
			case 'si/no':
				$val = ($val=='S')?'Si\'':'No';
				$align = 'center';
				break;
			default:
				$align = 'left';
				break;
		}			
		if ($tagA1!="" and trim($val)=="") {
			$val = "...";
		}
		$sHtml .= "<td class=\"$clRF\" style=\"text-align:$align;\">$tagA1$val$tagA2</td>";
		$tagA1 = $tagA2 = "";
	}
	$sHtml .= "<td class=\"$clRF\" style=\"text-align:left;\">"
		.$aRec["stato"]."&nbsp;-&nbsp;".substr($aRec["stato_descr"],0,30)."</td>";
	//link visualizza
	$sHref = "recVis.php?id=$idModulo&idr=".$aRec['id_rec']."&$sXSNQS";
	$sHtml .= "<td class=\"$clRF\" style=\"text-align:center;font-size:6pt;\">"
	."<a href=\"$sHref\" class='$clRF'>"
	."<img src='".OVF_URL_ROOT."/app/_img/popUp.png' border='0'/></a></td>";
	//link ZAP
	$sHref = "recZap.php?id=$idModulo&idr=".$aRec['id_rec']."&$sXSNQS";
	$sHtml .= "<td class=\"$clRF\" style=\"text-align:center;font-size:6pt;\">"
	."<a href=\"$sHref\" class='$clRF' style=\"font-size:6pt;\">"
	."<img src='".OVF_URL_ROOT."/app/_img/matita.png' border='0' height='10'/><br/>"
	.$aRec['cod_pagina']."</a></td>";
	return $sHtml;
}
